package redis

import (
	"d-serv-go/server/core/config"
	"d-serv-go/server/core/log"
	"github.com/gomodule/redigo/redis"
	"time"
)

var redisMap = map[string]*redis.Pool{}

func InitRedis() {
	//
	redisConfig := config.Instance.Redis

	for _, conf := range redisConfig {
		// 目前只支持单机redis
		pool := &redis.Pool{
			MaxActive: 100,
			MaxIdle: 5,
			IdleTimeout: time.Duration(100) * time.Second,
			Wait: true,
			Dial: func() (redis.Conn, error) {
				return redis.Dial("tcp", conf.Hosts, redis.DialDatabase(conf.Database), redis.DialPassword(conf.Password))
			},
		}
		redisMap[conf.Name] = pool
		log.Info("load redis:", conf.Name, "hosts:", conf.Hosts)
	}
}

func Redis(name string) *redis.Pool {
	return redisMap[name]
}
